package com.wondertek.web.controller.business;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wondertek.common.base.Result;
import com.wondertek.common.util.security.PreAuthorize;
import com.wondertek.service.dto.app.res.DeviceErrorRecordListRes;
import com.wondertek.service.dto.business.req.DerConfigAddReq;
import com.wondertek.service.dto.business.req.WebDerReq;
import com.wondertek.service.dto.business.res.DeviceErrorRecordMessageRes;
import com.wondertek.service.sdk.iotdh.dto.res.PushDhMessage;
import com.wondertek.service.service.business.DeviceErrorRecordMessageService;
import com.wondertek.service.service.business.DeviceErrorRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


/**
 * @author wangyu
 */
@RestController
@RequestMapping("/backend/business/alarm")
@Tag(name = "web报警")
public class DeviceErrorRecordController {

    @Autowired
    private DeviceErrorRecordService deviceErrorRecordService;

    @Autowired
    private DeviceErrorRecordMessageService deviceErrorRecordMessageService;

    /**
     * web查询报警
     *
     * @return
     */
    @PostMapping("/page")
    @Operation(summary = "web查询报警")
    @PreAuthorize("@el.check('backend:business:alarm:page')")
    public Result<Page<DeviceErrorRecordListRes>> page(@RequestBody WebDerReq req) {
        // req.setType(1);
        return Result.success(deviceErrorRecordService.findWebRecordList(req));
    }

    @PostMapping("/export")
    @Operation(summary = "导出报警信息")
    public void export(@RequestBody WebDerReq req, HttpServletResponse response) {

        deviceErrorRecordService.export(req, response);
    }


    @GetMapping("/updateReadById")
    @Operation(summary = "根据id修改已读状态")
    public Result updateReadById(@RequestParam("id") String id) {
        deviceErrorRecordService.updateWebReadById(id);
        return Result.success();
    }

    /**
     * 添加报警配置
     *
     * @param derConfigAddReq
     */
    @PostMapping("/addDerConfig")
    @Operation(summary = "添加报警配置")
    public Result addDerConfig(@RequestBody @Validated DerConfigAddReq derConfigAddReq) {
        deviceErrorRecordService.addDerConfig(derConfigAddReq);
        return Result.success();
    }

    /**
     * 查询报警配置
     *
     * @param deviceSn
     */
    @GetMapping("/findDerConfig")
    @Operation(summary = "查询报警配置")
    public Result findDerConfig(@RequestParam("deviceSn") String deviceSn) {
        return Result.success(deviceErrorRecordService.findDerConfig(deviceSn));
    }

    /**
     * 根据userId查询告警站内消息
     *
     * @return
     */
    @GetMapping("/findMessageByUserId")
    @Operation(summary = "根据userId查询告警站内消息")
    public Result<Page<DeviceErrorRecordMessageRes>> findMessageByUserId(@RequestParam("pageNo") Integer pageNo, @RequestParam("pageSize") Integer pageSize) {
        return Result.success(deviceErrorRecordMessageService.findMessageByUserId(pageNo, pageSize));
    }

    /**
     * 根据id修改告警站内消息为已读状态
     *
     * @param id
     */
    @GetMapping("/updateMessageReadById")
    @Operation(summary = "根据id修改告警站内消息为已读状态")
    public Result updateMessageReadById(@RequestParam("id") String id) {
        deviceErrorRecordMessageService.updateMessageReadById(id);
        return Result.success();
    }


    @GetMapping("/test")
    @Operation(summary = "回调测试")
    public void test(@RequestParam("snCode") String snCode, @RequestParam("type") Integer type, @RequestParam("msgType") String msgType) {

        PushDhMessage pushDhMessage = new PushDhMessage();

        pushDhMessage.setDeviceSn(snCode);
        pushDhMessage.setType(type);
        pushDhMessage.setStatus(1);
        pushDhMessage.setAlarmTime(new Date().getTime());
        pushDhMessage.setMsgType(msgType);
        pushDhMessage.setName("告警手动回调测试名称");

        deviceErrorRecordService.rollBackErrorRecord(pushDhMessage);
    }

}
